#include"Queue.h"
void QueueInit(Queue* pq)
{
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QueueNode* new =(QueueNode*) malloc(sizeof(QueueNode));
	if (new == NULL)
	{
		perror("malloc fail\n");
		exit(1);
	}
	new->data = x;
	new->next = NULL;
	if (pq->phead == NULL)
	{
		pq->phead = pq->ptail = new;
	}
	else 
	{
		pq->ptail->next = new;
		pq->ptail = new;
		
	}
	pq->size++;
}
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->phead == NULL;
}
int	QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (pq->phead == pq->ptail)
	{
		free(pq->phead);
		pq->phead = NULL;
		pq->ptail = NULL;
	}
	else {
		Queue* pcur = pq->phead->next;
		free(pq->phead);
		pq->phead = pcur;
	}
	--pq->size;
}
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->phead->data;
}
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->ptail->data;
}
void QueueDestroy(Queue* pq)
{
	assert(pq);
	while (pq->phead)
	{ 
		QueueNode* new = pq->phead->next;
		free(pq->phead);
		pq->phead = new;
	}
	pq->ptail = NULL;
	pq->size = 0;

}